﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Clients_Page : System.Web.UI.MasterPage
{

    private String strTop = "<a href='" + Constant.ROOT_PATH + "$link'><span>$text</span></a>";
    private String strBottom = "<a href='" + Constant.ROOT_PATH + "$link'>$text</a>";
    private String strSub = "<a href='" + Constant.ROOT_PATH + "$link'>$text</a>";
    private String strTab = "<a href='" + Constant.ROOT_PATH + "$link'>$text</a>";
    private String strQuangCao = "<a href='$link'><img class='ads_item' src='"+ Constant.ROOT_PATH + "$src' /></a>";
    private String strProduct = "<li><a href='" + Constant.ROOT_PATH+"$link'><b>$text</b></a></li>";
    private String strYahoo = "<div align='left' style='vertical-align: middle;padding-top:5px;'><a href='ymsgr:sendim?$Nick'>"
        +"<img src='http://opi.yahoo.com/online?u=$Nick&m=g&t=1' style=' padding-right:5px;'  alt='$'/></a>$text"
        + " </div><div> <img src='"+Constant.ROOT_PATH + " Clients/images/Phone.gif' border='0' style='width: 17px;padding-right:30px;padding-left:25px; padding-bottom:5px; border-width: 0px;'"
        +"align='left' alt='$text'/><span>$phone</span></div>";

    private void LoadDataYahoo()
    {

        WebData db = new WebData();
        List<DataLibrary.tbl_Nick_Yahoo> list = db.tbl_Nick_Yahoos.GetListCache("nick_yahoo", 60);
        this.grdYahoo.DataSource = list;
        this.grdYahoo.DataBind();
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
     
            LoadDataProduct();
            LoadDataYahoo();
            LoadLink();
            LoadDataCategory();
            LoadFooter();
            LoadWebTop();
            LoadTabs();
            LoadQuangCao();
            //String url = Request.RawUrl;
            //if (url == "/" || url == Constant.ROOT_PATH)
            //{
            //    Response.Redirect(Constant.ROOT_PATH + "Default");
            //}
        }
    }

    private void LoadTabs() {
        WebData db = new WebData();
        List<DataLibrary.tbl_Tabs> list = db.tbl_Tabss.GetListCache("tabs",60);
        String url = "";
       strTab = "<a href='" + Constant.ROOT_PATH + "$link'>$text</a>";
        for (int i = 0; i < list.Count; i++) { 
            if(!String.IsNullOrEmpty(url)){
                url=url + " | ";
            }
            url = url + strTab.Replace("$link", list[i].TabUrl).Replace("$text", list[i].TabText);
        }
        lblTabs.Text = url;
    }

    private void LoadWebTop() {
        String strPage = "";
        String url = "";
        try
        {
            strPage = Request.Params["Pro"].ToString().Trim();
        }
        catch
        {
            
        }
        FriendlyUrl itemUrl = Constant.GetItem(strPage);
        String strTitle = "";
        if (itemUrl != null) {
            strPage =itemUrl.Url;
            strTitle = itemUrl.FriendUrl;
        }
        
        if (!String.IsNullOrEmpty(strPage)) {
            if (strPage.ToLower() == "download".ToLower() || strPage.ToLower() == "down".ToLower())
            {
                strPage = "download";
                itemUrl = Constant.GetItem(strPage);
                if (itemUrl != null)
                {
                 //   strPage = itemUrl.Url;
                    strTitle = itemUrl.FriendUrl;
                }
                WebData data=new WebData();
                String strSql = "select * from tbl_Group_Download order by GroupOrderID";
                DataTable tb = data.tbl_Downloads.GetDataTableCache("groupDownload", Constant.time, strSql, CommandType.Text);
                foreach (DataRow row in tb.Rows) {
                    String sText = row["GroupCaption"].ToString();
                    if (!String.IsNullOrEmpty(url)) {
                        url = url + " | ";
                    }
                    String s = String.Format("{0}/{1}/{2}",strTitle,row["GroupID"].ToString(),Constant.ChangeString(sText));
                    url = url + strTop.Replace("$text", sText).Replace("$link", s);
                }
            }
            else
            {
                using (DamiWebDataContext db = new DamiWebDataContext())
                {
                    var query = from p in db.tbl_TopUrls
                                where p.Url == strPage
                                select p;

                    foreach (var c in query)
                    {
                        String[] strUrl = c.topUrl.ToString().Split(';');
                        String[] strText = c.topText.ToString().Split(';');
                        int i = 0;
                        for (i = 0; i < strUrl.Length; i++)
                        {
                            if (i != 0)
                            {
                                url = url + " | ";
                            }
                            itemUrl = Constant.GetItem(strUrl[i].Trim());
                            String s="";
                            if (itemUrl!=null) {
                                s = itemUrl.FriendUrl;
                            }
                            url = url + strTop.Replace("$text", strText[i].Trim()).Replace("$link",s );
                        }
                    }
                }
            }
        }

        this.lblTop.Text = url;
    }

    private void LoadDataCategory() {
        String url = "";
        String urlBottom = "";

        
            WebData data=new WebData();
            DataTable tb = data.tbl_Categorys.GetDataTableCache("category",Constant.time, "GetCategory",
                CommandType.StoredProcedure);

            foreach (DataRow row in tb.Rows)
            {
                url = url + strTop.Replace("$text", row["nameCat"].ToString()).Replace("$link", row["FriendlyUrl"].ToString().Replace("~/", "").Replace(".aspx", ""));
                if (!string.IsNullOrEmpty(urlBottom))
                {
                    urlBottom += " | ";
                }
                urlBottom = urlBottom + strBottom.Replace("$text", row["nameCat"].ToString()).Replace("$link", row["FriendlyUrl"].ToString().Replace("~/", "").Replace(".aspx", ""));
            }
        
        this.mainTop.Text = url;
        this.bottom.Text = urlBottom;

    }

    private void LoadDataProduct() {

        WebData data = new WebData();
        DataTable tb = data.tbl_Products.GetDataTableCache("productPage", 60, "GetProducts", CommandType.StoredProcedure);
        String url = "";
        strProduct = "<li><a href='" + Constant.ROOT_PATH + "$link'><b>$text</b></a></li>";
        foreach (DataRow row in tb.Rows)
        {
            String strLink = String.Format("{0}/{1}/{2}",Constant.strSanpham ,row["idProduct"].ToString(), Constant.ChangeString(row["Description"].ToString()));
            String strText = row["Description"].ToString().Trim();
            url = url + strProduct.Replace("$link",strLink).Replace("$text",strText);
        }
        lblProduct.Text = url;
    }

    

    private void LoadLink() {

        WebData data = new WebData();
        List<DataLibrary.tbl_Weblink> list = data.tbl_Weblinks.GetListCache("weblink",60);
        this.drlLink.DataSource = list;
        this.drlLink.DataValueField = "Url";
        this.drlLink.DataTextField = "Title";
        this.drlLink.DataBind();
        this.drlLink.Items.Insert(0, new ListItem("------- Liên kết website --------", "0"));
    }

    private void LoadQuangCao() {
        WebData data = new WebData();
        List<DataLibrary.tbl_Quangcao> list = data.tbl_Quangcaos.GetAll().FindAll(p=>p.IsActive==1);
        String url = "";
        foreach (DataLibrary.tbl_Quangcao obj in list) {
            url = url + strQuangCao.Replace("$link", obj.UrlQuangcao).Replace("$src",obj.Image);
        }
        lblQuangCao.Text = url;
    }

    private void LoadFooter() {
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.SysDamiWebs
                        select p;
            foreach (var c in query)
            {
                lblFooter.Text = c.footer;
            }
        }
    }

   
}
